package com.weipai.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.DLeadFloorMapper;
import com.weipai.demo.mapper.DLoansNewsMapper;
import com.weipai.demo.mapper.base.LoansNewsMapper;
import com.weipai.demo.model.LoansNews;
import com.weipai.demo.service.LoansnewsService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: 赵项阳
 * @Date: 2019/7/2 9:25
 * @Version 1.0
 */
@Service
public class LoansnewsServiceImpl implements LoansnewsService {

    @Autowired
    private LoansNewsMapper loansNewsMapper;
    @Autowired
    private DLoansNewsMapper dLoansNewsMapper;
    @Autowired
    private DLeadFloorMapper dLeadFloorMapper;

    //添加或修改文章
    @Override
    public Response save(LoansNews loansNews) {
        Response response=new Response();
        Date date=new Date();
        loansNews.setCreateTime(date);
        if (loansNews.getId()==null){
            int insert = loansNewsMapper.insert(loansNews);
            response.setCode(RetCode.SUCCESS);
        }else {
           dLoansNewsMapper.updateById(loansNews);
           response.setCode(RetCode.UPDATESUCCESS);
        }

        return response;
    }



    //保存图片
    @Override
    public Response savePicture(LoansNews loansNews) {
        Response response = new Response();
        if(loansNews.getId()==null){
            loansNewsMapper.insert(loansNews);
            response.setCode(RetCode.SUCCESS);

        }else {
            // int i1 = pictureShowMapper.updateByPrimaryKey(pictureShow);
            int i = loansNewsMapper.updateByPrimaryKeySelective(loansNews);
            if (i>0){
                response.setCode(RetCode.UPDATESUCCESS);
            }else {
                response.setCode(RetCode.UPDATEERROR);
                return response;
            }
        }
        return response;
    }

    //==================================================================================================================

    //分页展示文章不包括内容
    @Override
    public LayuiTablePagingResult show(Integer page, Integer size,Integer news_class) {
        LayuiTablePagingResult result=null;
        try {
            PageHelper.startPage(page,size);
            LoansNews loans = new LoansNews();
            loans.setNewsClass(Integer.toString(news_class));
            List<Map<String,Object>> loansNews = dLoansNewsMapper.selectNoContent(loans);
            PageInfo<Map<String,Object>> pageInfo=new PageInfo<>(loansNews);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
        } catch (Exception e) {
            result.setCode(RetCode.REQUEST_ERROR);
            result.setData(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    //分页展示文章不包括内容
    @Override
    public LayuiTablePagingResult recommendNews(Integer page, Integer size) {
        LayuiTablePagingResult result=null;
        try {
            PageHelper.startPage(page,size);
//            List<LeadFloor> leadFloors = dLeadFloorMapper.selectAll(1);
//            List<LoansNews> loansNews = dLoansNewsMapper.selectRecommendNews(leadFloors);

            List<Map<String,Object>> loansNews = dLoansNewsMapper.selectRecommendNews(1);
            PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(loansNews);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
        } catch (Exception e) {
            result.setCode(RetCode.REQUEST_ERROR);
            result.setData(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    //展示文章包含内容
    @Override
    public Response showByContent(Integer id) {
        Response response=new Response();

        //如果id不为空,查询包含内容的文章
        if (id!=null){
            LoansNews loansNews=new LoansNews();
            loansNews.setId(id);
            LoansNews newsList = loansNewsMapper.selectOne(loansNews);
            response.setData(newsList);
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
            return response;
        }
        return response;
    }

    //==================================================================================================================

    //根据id删除
    @Override
    public Response deleteById(Integer id) {
        Response response=new Response();
        if (id !=null){
            LoansNews loansNews=new LoansNews();
            loansNews.setId(id);
            int delete = loansNewsMapper.delete(loansNews);
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.ID_NULL);
            return response;
        }
        return response;
    }


}
